package com.welink.cmp.topotree;

import com.welink.cmp.dao.tree.dataobejct.DeviceTopologyTreeDO;
import com.welink.cmp.domain.topotree.DeviceNetTopoDomain;
import com.welink.cmp.dto.IdQuery;
import com.welink.cmp.dto.metadata.AssetsNameQry;
import com.welink.cmp.dto.topotree.BuildNetTopologyAddCmd;
import com.welink.cmp.dto.topotree.DeviceTopoNodeCO;
import com.welink.cmp.dto.topotree.ModifyNetTopologyUptCmd;
import com.welink.cmp.topotree.command.NetTopologyCmdExe;
import com.welink.common.dto.Response;
import com.welink.common.dto.SingleResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 网络拓扑服务API
 *
 * @author yanwei li create  2024-05-24 17:34
 */
@Service
public class NetTopologyAppService {

    @Autowired
    private NetTopologyCmdExe netTopologyCmdExe;

    /**
     * [新增]构建网络拓扑关系
     * @return resultCode
     */
    public SingleResponse<Long> buildNetTopology(BuildNetTopologyAddCmd addCmd){
        final Long id = netTopologyCmdExe.save(addCmd);
        return SingleResponse.of(id);

    }

    /**
     * [修改] 修改当前机器的相邻机器信息
     * @param uptCmd 相邻机器信息
     * @return resultCode
     */
    public Response modifyNext(ModifyNetTopologyUptCmd uptCmd){
        final Boolean update = netTopologyCmdExe.update(uptCmd);

        return Response.buildSuccess();
    }

    /**
     * [ID 查询] 查询当前节点下的交换机信息
     * @param nameQry ID
     * @return DTO
     */
    public SingleResponse<DeviceNetTopoDomain> getSwitchNodesItemByName(AssetsNameQry nameQry){
        final DeviceNetTopoDomain domain = netTopologyCmdExe.querySwitchNodesByName(nameQry.getName());
        return SingleResponse.of(domain);
    }

    /**
     * [列表查询] 获取所有子节点相邻机器
     * @param nameQry 机器名
     * @return 相邻机器名列表
     */
    public SingleResponse<DeviceNetTopoDomain> getHostNodesItemByName(AssetsNameQry nameQry){
        final DeviceNetTopoDomain domain = netTopologyCmdExe.queryHostsNodesByName(nameQry.getName());
        return SingleResponse.of(domain);
    }

    /**
     * [拓扑树查询]
     */
    public DeviceTopoNodeCO expansionNodes(IdQuery idQuery){
        DeviceTopoNodeCO result = new DeviceTopoNodeCO();
        final DeviceTopologyTreeDO byId = netTopologyCmdExe.getById(idQuery.getId());
        final List<Long> subNodeIds = netTopologyCmdExe.expansionNodes(idQuery.getId());
        result.setLocalDevice(byId.getLocalDevice());
        result.setLocalInterfaceMaxBandwidth(byId.getLocalInterfaceMaxBandwidth());
        result.setLocalInterfaceType(byId.getLocalInterfaceType());
        result.setLocalInterface(byId.getLocalInterface());
        result.setSubNodes(subNodeIds);
        return result;
    }

}
